/**
 * Created by ayakatsune on 14-7-29.
 */
var showErrorTooltip = function (obj, errorList) {
    $.each(obj.successList, function (index, value) {
        return $(value).popover("hide");
    });
    return $.each(errorList, function (index, value) {
        var _popover;
        _popover = $(value.element).popover({
            trigger: "manual",
            placement: "top",
            content: value.message,
            template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
        });
        _popover.data("bs.popover").options.content = value.message;
        return $(value.element).popover("show");
    });
}
var refreshCode = function (obj) {
    obj.src = "/validcode?" + Math.random();
}
var createItemDialog = null;
var createItem = function (step, newWin) {
    var title = "";
    console.log(step);
    var url = "";
    switch (step) {
        case "projectPanel":
            url = "newProject"
            title = "新建项目";
            break;
        case "teamPanel":
            title = "新建团队";
            url = "newTeam"
            break;
        default:
            break;
    }
    if (newWin == 1)
        createItemDialog = BootstrapDialog.show({
            title: title,
            message: $('<div id="newFrm"></div>').load(url),
            buttons:[{
                label:"创建",
                action:function(defDialog)
                {
                    $("#newFrm form").submit();
                }
            }]
        });
    if (newWin == 0) {
        title = "<div href='javascript:void(0)' style='cursor: pointer;' onclick='createItem(\"projectPanel\",-1)'><i class='fa fa-chevron-left'></i>  " + title + "</div>";
    }
    if (newWin == 0 || newWin == -1)
        createItemDialog.setMessage($('<div id="newFrm"></div>').load(url))
    createItemDialog.setTitle(title);
}
var closeItemDialog=function()
{
    createItemDialog.close();
}
var newTeamHandle=function()
{
    $('#newFrm form').validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        showErrors: function (errorMap, errorList) {
            return showErrorTooltip(this, errorList);
        },
        submitHandler: function (form) {
            alert("ssd");
            var params = $(form).serialize();
            $.ajax({
                url: '/api/newTeam',
                data: params,
                dataType: "json",
                type:"post",
                //contentType:"application/json",
                success:function(data, textStatus, jqXHR) {
                    if("SUCCESS"==data.status)
                        location.href="index";
                    else
                        alert(data.msg);
                },
                error:function(data, textStatus, jqXHR)
                {
                    alert(textStatus);
                }
            });
            return false;
        }
    });

    $('.login-form input').keypress(function (e) {
        if (e.which == 13) {
            if ($('#newFrm form').validate().form()) {
                $('#newFrm form').submit();
            }
            return false;
        }
    });

}
var newProjectHandle=function()
{
    $('#newFrm form').validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        rules: {
            teamName: {
                required: true
            }
        },
        messages: {
            teamName: {
                required: "团队名称不能为空."
            }
        },
        showErrors: function (errorMap, errorList) {
            return showErrorTooltip(this, errorList);
        },
        submitHandler: function (form) {
            alert("ssd");
            var params = $(form).serialize();
            $.ajax({
                url: '/api/newTeam',
                data: params,
                dataType: "json",
                type:"post",
                //contentType:"application/json",
                success:function(data, textStatus, jqXHR) {
                    if("SUCCESS"==data.status)
                        location.href="index";
                    else
                        alert(data.msg);
                },
                error:function(data, textStatus, jqXHR)
                {
                    alert(textStatus);
                }
            });
            return false;
        }
    });

    $('.login-form input').keypress(function (e) {
        if (e.which == 13) {
            if ($('#newFrm form').validate().form()) {
                $('#newFrm form').submit();
            }
            return false;
        }
    });

}